package com.sc.lindada.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sc.lindada.model.dto.question.QuestionContentDTO;
import com.sc.lindada.model.entity.App;
import com.sc.lindada.model.entity.Question;
import com.sc.lindada.model.entity.ScoringResult;
import com.sc.lindada.model.entity.UserAnswer;
import com.sc.lindada.model.vo.QuestionVO;
import com.sc.lindada.service.AppService;
import com.sc.lindada.service.QuestionService;
import com.sc.lindada.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 自定义测评
 */
@ScoringStrategyConfig(appType = 1,scoringStrategy = 0)
public class CustomTestScoringStrategy implements ScoringStrategy {
    @Resource
    private AppService appService;

    @Resource
    private QuestionService questionService;
    @Resource
    private ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
//        1.根据id查询题目和题目信息
        Long appId = app.getId();
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, appId)
        );
        List<ScoringResult> scoringResultList = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class).eq(ScoringResult::getAppId, appId)
        );
//        2.统计每个题目选择的选项
        Map<String,Integer> optionCount = new HashMap<>();
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();
        for (QuestionContentDTO questionContentDTO : questionContent) {
            // 遍历答案列表
            for (String answer : choices) {
                // 遍历题目中的选项
                for (QuestionContentDTO.Option option : questionContentDTO.getOptions()) {
                    // 如果答案和选项的key匹配
                    if (option.getKey().equals(answer)) {
                        // 获取选项的result属性
                        String result = option.getResult();
                        // 如果result属性不在optionCount中，初始化为0
                        if (!optionCount.containsKey(result)) {
                            optionCount.put(result, 0);
                        }
                        // 在optionCount中增加计数
                        optionCount.put(result, optionCount.get(result) + 1);
                    }
                }
            }
        }
//        3.遍历每种评分的最高结果
        // 初始化最高分数为0，以及对应的最高分数结果
        int maxScore = 0;
        ScoringResult maxscoringResult = scoringResultList.get(0);
        // 遍历所有评分结果
        for (ScoringResult scoringResult : scoringResultList){
            // 将结果属性转换为字符串列表
            List<String> resultProp = JSONUtil.toList(scoringResult.getResultProp(),String.class);
            // 计算当前评分结果的分数总和
            int score = resultProp.stream()
                    .mapToInt(prop->optionCount.getOrDefault(prop,0))
                    .sum();
            // 如果当前分数高于最高分数，则更新最高分数及对应的评分结果
            if (score>maxScore){
                maxScore = score;
                maxscoringResult = scoringResult;
            }
        }
        UserAnswer userAnswer = new UserAnswer();

        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxscoringResult.getId());
        userAnswer.setResultName(maxscoringResult.getResultName());
        userAnswer.setResultDesc(maxscoringResult.getResultDesc());
        userAnswer.setResultPicture(maxscoringResult.getResultPicture());

        return userAnswer;
    }
}
